#include <iostream>
#include "DynamicArray.h"


DynamicArray::DynamicArray()
{
    size = 10;
    len = 0;
    a = new int[size];
    
}

DynamicArray::DynamicArray(const DynamicArray &array)
{
    std::cout<<"调用深拷贝函数"<<std::endl;
    len = array.len;
    size = array.size;
    a= new int[size];
    for(int i = 0; i < len ;i++)
    {
        a[i] = array.a[i];
    }
}

DynamicArray::~DynamicArray()
{
    delete []a;
    size = 0;
    len = 0;
    std::cout<<"调用析构函数"<<std::endl;
}

void DynamicArray::InsertTail(int element)
{
    if(len == size)
    {
        size *= 2;
        int *newPtr = new int(size);
        for(int i = 0; i < len; i++)
            newPtr[i] = a[i];
        delete []a;
        a = newPtr;
    } 
    a[len++] = element;
}

void DynamicArray::show()
{
    std::cout<<"size : "<<size<<" len : "<<len<<std::endl;
    for(int i = 0; i < len; i++)
    {
        std::cout<<a[i]<<" ";
    }
    std::cout<<std::endl;
}
